package com.zhuhjay.utils.result;

import com.zhuhjay.enums.HttpCode;

import static com.zhuhjay.enums.HttpCode.*;

/**
 * 当返回的数据是一个对象时，可以使用此类( @Jackson 注解可生效)
 * @author ZhuHJay
 * @date 2023/2/12 20:08
 */
public class ResultBean implements Result {
    private int code;
    private String message;
    private Object data;

    public ResultBean(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResultBean ok() {
        return new ResultBean(SUCCESS.getCode(), SUCCESS.getDesc());
    }

    public static ResultBean ok(HttpCode code) {
        return new ResultBean(code.getCode(), code.getDesc());
    }

    public static ResultBean ok(String message) {
        return new ResultBean(SUCCESS.getCode(), message);
    }

    public static ResultBean fail() {
        return new ResultBean(SERVER_ERROR.getCode(), SERVER_ERROR.getDesc());
    }

    public static ResultBean fail(HttpCode code) {
        return new ResultBean(code.getCode(), code.getDesc());
    }

    public static ResultBean fail(String message) {
        return new ResultBean(USER_ERROR.getCode(), message);
    }

    public ResultBean data(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public Object getData() {
        return data;
    }
}
